<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分布式系统一致性协议详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
            background-color: #f9fafb;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 50%, #a78bfa 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .mermaid {
            background-color: white;
            border-radius: 0.5rem;
            padding: 1.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
        .highlight {
            position: relative;
        }
        .highlight:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 30%;
            background-color: rgba(167, 139, 250, 0.3);
            z-index: -1;
            transition: all 0.2s ease;
        }
        .highlight:hover:after {
            height: 100%;
        }
        .section-divider {
            position: relative;
            height: 80px;
            overflow: hidden;
        }
        .section-divider svg {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 md:py-32 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold leading-tight mb-4">分布式系统一致性协议</h1>
                    <p class="text-xl md:text-2xl opacity-90 mb-8">确保数据同步与协调的关键技术</p>
                    <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-sm rounded-lg p-6 max-w-md">
                        <p class="italic">"在分布式系统中，一致性是确保多个节点之间数据同步和协调的关键问题。分布式一致性协议是解决这一问题的核心工具。"</p>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="mermaid w-full max-w-md">
                        graph TD
                            A[一致性协议] --> B[Paxos]
                            A --> C[Raft]
                            A --> D[Zab]
                            B --> E[强一致性]
                            B --> F[复杂实现]
                            C --> G[易于理解]
                            C --> H[领导选举]
                            D --> I[高性能]
                            D --> J[Zookeeper]
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl px-4 py-16">
        <!-- Section 1 -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="bg-indigo-500 w-2 h-12 mr-4 rounded-full"></div>
                <h2 class="text-3xl font-bold">1. 分布式一致性问题</h2>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-question-circle text-indigo-500 mr-3"></i>1.1 什么是一致性？
                    </h3>
                    <p class="text-gray-700">一致性是指在分布式系统中，多个节点对同一份数据的视图保持一致。例如，在一个分布式数据库中，所有节点对某个数据的读取结果应该相同。</p>
                </div>
                
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-exclamation-triangle text-amber-500 mr-3"></i>1.2 一致性的挑战
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-network-wired text-purple-500 mt-1 mr-2"></i>
                            <span><strong>网络分区：</strong>节点之间的网络连接可能中断，导致部分节点无法通信。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-server text-red-500 mt-1 mr-2"></i>
                            <span><strong>节点故障：</strong>节点可能发生故障，导致数据丢失或无法响应请求。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-code-branch text-blue-500 mt-1 mr-2"></i>
                            <span><strong>并发操作：</strong>多个节点可能同时修改同一份数据，导致冲突。</span>
                        </li>
                    </ul>
                </div>
            </div>

            <div class="mermaid">
                flowchart LR
                    subgraph 一致性挑战
                        A[网络分区] --> B[通信中断]
                        C[节点故障] --> D[数据丢失]
                        E[并发操作] --> F[数据冲突]
                    end
            </div>
        </section>

        <!-- Divider -->
        <div class="section-divider my-16">
            <svg viewBox="0 0 1200 120" preserveAspectRatio="none">
                <path d="M0,0V46.29c47.79,22.2,103.59,32.17,158,28,70.36-5.37,136.33-33.31,206.8-37.5C438.64,32.43,512.34,53.67,583,72.05c69.27,18,138.3,24.88,209.4,13.08,36.15-6,69.85-17.84,104.45-29.34C989.49,25,1113-14.29,1200,52.47V0Z" opacity=".25" fill="#7c3aed"></path>
                <path d="M0,0V15.81C13,36.92,27.64,56.86,47.69,72.05,99.41,111.27,165,111,224.58,91.58c31.15-10.15,60.09-26.07,89.67-39.8,40.92-19,84.73-46,130.83-49.67,36.26-2.85,70.9,9.42,98.6,31.56,31.77,25.39,62.32,62,103.63,73,40.44,10.79,81.35-6.69,119.13-24.28s75.16-39,116.92-43.05c59.73-5.85,113.28,22.88,168.9,38.84,30.2,8.66,59,6.17,87.09-7.5,22.43-10.89,48-26.93,60.65-49.24V0Z" opacity=".5" fill="#7c3aed"></path>
                <path d="M0,0V5.63C149.93,59,314.09,71.32,475.83,42.57c43-7.64,84.23-20.12,127.61-26.46,59-8.63,112.48,12.24,165.56,35.4C827.93,77.22,886,95.24,951.2,90c86.53-7,172.46-45.71,248.8-84.81V0Z" fill="#7c3aed"></path>
            </svg>
        </div>

        <!-- Section 2 -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="bg-indigo-500 w-2 h-12 mr-4 rounded-full"></div>
                <h2 class="text-3xl font-bold">2. Paxos 协议</h2>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-lightbulb text-blue-500 mr-3"></i>2.1 Paxos 的基本概念
                    </h3>
                    <p class="text-gray-700">Paxos 是一种分布式一致性算法，由 Leslie Lamport 提出。Paxos 通过多轮投票的方式，确保多个节点就某个值达成一致。</p>
                </div>
                
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-users text-teal-500 mr-3"></i>2.2 Paxos 的角色
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-user-shield text-indigo-500 mt-1 mr-2"></i>
                            <span><strong>Proposer：</strong>提出提案的节点。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-user-check text-green-500 mt-1 mr-2"></i>
                            <span><strong>Acceptor：</strong>接受或拒绝提案的节点。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-user-graduate text-purple-500 mt-1 mr-2"></i>
                            <span><strong>Learner：</strong>学习最终达成一致的值的节点。</span>
                        </li>
                    </ul>
                </div>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-project-diagram text-orange-500 mr-3"></i>2.3 Paxos 的工作流程
                    </h3>
                    <ol class="list-decimal list-inside space-y-2 text-gray-700 pl-2">
                        <li><span class="highlight">Prepare 阶段</span>：Proposer 向 Acceptor 发送 Prepare 请求，请求接受提案。</li>
                        <li><span class="highlight">Promise 阶段</span>：Acceptor 收到 Prepare 请求后，如果接受提案，则返回 Promise 响应。</li>
                        <li><span class="highlight">Accept 阶段</span>：Proposer 收到多数 Acceptor 的 Promise 响应后，向 Acceptor 发送 Accept 请求，请求接受提案。</li>
                        <li><span class="highlight">Accepted 阶段</span>：Acceptor 收到 Accept 请求后，如果接受提案，则返回 Accepted 响应。</li>
                        <li><span class="highlight">Learn 阶段</span>：Learner 从 Acceptor 获取最终达成一致的值。</li>
                    </ol>
                </div>
                
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-balance-scale text-pink-500 mr-3"></i>2.4 Paxos 的优缺点
                    </h3>
                    <div class="grid grid-cols-1 gap-4">
                        <div class="bg-green-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-green-700 flex items-center mb-2">
                                <i class="fas fa-thumbs-up mr-2"></i>优点
                            </h4>
                            <p class="text-green-700">Paxos 是一种强一致性协议，能够保证数据的一致性。</p>
                        </div>
                        <div class="bg-red-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-red-700 flex items-center mb-2">
                                <i class="fas fa-thumbs-down mr-2"></i>缺点
                            </h4>
                            <p class="text-red-700">Paxos 的实现复杂，难以理解和调试。</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mermaid mt-8">
                sequenceDiagram
                    participant Proposer
                    participant Acceptor
                    participant Learner
                    
                    Proposer->>Acceptor: Prepare 请求
                    Acceptor-->>Proposer: Promise 响应
                    Proposer->>Acceptor: Accept 请求
                    Acceptor-->>Proposer: Accepted 响应
                    Acceptor->>Learner: 通知最终值
            </div>
        </section>

        <!-- Section 3 -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="bg-indigo-500 w-2 h-12 mr-4 rounded-full"></div>
                <h2 class="text-3xl font-bold">3. Raft 协议</h2>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-lightbulb text-blue-500 mr-3"></i>3.1 Raft 的基本概念
                    </h3>
                    <p class="text-gray-700">Raft 是一种分布式一致性算法，由 Diego Ongaro 和 John Ousterhout 提出。Raft 通过选举领导者和日志复制的方式，确保多个节点就某个值达成一致。</p>
                </div>
                
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-users text-teal-500 mr-3"></i>3.2 Raft 的角色
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-crown text-yellow-500 mt-1 mr-2"></i>
                            <span><strong>Leader：</strong>负责处理客户端请求和日志复制的节点。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-user-friends text-blue-500 mt-1 mr-2"></i>
                            <span><strong>Follower：</strong>被动接收 Leader 的日志复制请求的节点。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-vote-yea text-green-500 mt-1 mr-2"></i>
                            <span><strong>Candidate：</strong>在选举过程中竞争成为 Leader 的节点。</span>
                        </li>
                    </ul>
                </div>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-project-diagram text-orange-500 mr-3"></i>3.3 Raft 的工作流程
                    </h3>
                    <ol class="list-decimal list-inside space-y-2 text-gray-700 pl-2">
                        <li><span class="highlight">Leader 选举</span>：当 Follower 检测到 Leader 失效时，发起选举，成为 Candidate。Candidate 向其他节点发送投票请求，如果获得多数票，则成为 Leader。</li>
                        <li><span class="highlight">日志复制</span>：Leader 接收客户端请求，将请求追加到日志中，并向 Follower 发送日志复制请求。Follower 收到日志复制请求后，将日志追加到本地日志中，并返回确认响应。</li>
                        <li><span class="highlight">提交日志</span>：Leader 收到多数 Follower 的确认响应后，将日志提交，并通知 Follower 提交日志。</li>
                    </ol>
                </div>
                
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-balance-scale text-pink-500 mr-3"></i>3.4 Raft 的优缺点
                    </h3>
                    <div class="grid grid-cols-1 gap-4">
                        <div class="bg-green-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-green-700 flex items-center mb-2">
                                <i class="fas fa-thumbs-up mr-2"></i>优点
                            </h4>
                            <p class="text-green-700">Raft 的实现相对简单，易于理解和调试。</p>
                        </div>
                        <div class="bg-red-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-red-700 flex items-center mb-2">
                                <i class="fas fa-thumbs-down mr-2"></i>缺点
                            </h4>
                            <p class="text-red-700">Raft 的性能受 Leader 的影响较大，Leader 失效时需要进行选举，可能导致短暂的不可用。</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mermaid mt-8">
                stateDiagram-v2
                    [*] --> Follower
                    Follower --> Candidate: 选举超时
                    Candidate --> Leader: 获得多数票
                    Leader --> Follower: 发现更高任期
                    Candidate --> Follower: 发现更高任期
                    Candidate --> Candidate: 选举超时(重新选举)
            </div>
        </section>

        <!-- Section 4 -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="bg-indigo-500 w-2 h-12 mr-4 rounded-full"></div>
                <h2 class="text-3xl font-bold">4. Zab 协议</h2>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-lightbulb text-blue-500 mr-3"></i>4.1 Zab 的基本概念
                    </h3>
                    <p class="text-gray-700">Zab 是一种分布式一致性算法，由 Apache Zookeeper 使用。Zab 通过选举领导者和原子广播的方式，确保多个节点就某个值达成一致。</p>
                </div>
                
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-users text-teal-500 mr-3"></i>4.2 Zab 的角色
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-crown text-yellow-500 mt-1 mr-2"></i>
                            <span><strong>Leader：</strong>负责处理客户端请求和原子广播的节点。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-user-friends text-blue-500 mt-1 mr-2"></i>
                            <span><strong>Follower：</strong>被动接收 Leader 的原子广播请求的节点。</span>
                        </li>
                    </ul>
                </div>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-project-diagram text-orange-500 mr-3"></i>4.3 Zab 的工作流程
                    </h3>
                    <ol class="list-decimal list-inside space-y-2 text-gray-700 pl-2">
                        <li><span class="highlight">Leader 选举</span>：当 Follower 检测到 Leader 失效时，发起选举，成为 Candidate。Candidate 向其他节点发送投票请求，如果获得多数票，则成为 Leader。</li>
                        <li><span class="highlight">原子广播</span>：Leader 接收客户端请求，将请求追加到日志中，并向 Follower 发送原子广播请求。Follower 收到原子广播请求后，将日志追加到本地日志中，并返回确认响应。</li>
                        <li><span class="highlight">提交日志</span>：Leader 收到多数 Follower 的确认响应后，将日志提交，并通知 Follower 提交日志。</li>
                    </ol>
                </div>
                
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-balance-scale text-pink-500 mr-3"></i>4.4 Zab 的优缺点
                    </h3>
                    <div class="grid grid-cols-1 gap-4">
                        <div class="bg-green-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-green-700 flex items-center mb-2">
                                <i class="fas fa-thumbs-up mr-2"></i>优点
                            </h4>
                            <p class="text-green-700">Zab 的性能较高，能够快速达成一致。</p>
                        </div>
                        <div class="bg-red-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-red-700 flex items-center mb-2">
                                <i class="fas fa-thumbs-down mr-2"></i>缺点
                            </h4>
                            <p class="text-red-700">Zab 的实现复杂，难以理解和调试。</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mermaid mt-8">
                gantt
                    title Zab 协议时间线
                    dateFormat  YYYY-MM-DD
                    section 选举阶段
                    选举开始           :a1, 2023-01-01, 1d
                    投票过程           :after a1, 2d
                    section 广播阶段
                    原子广播开始       :a2, after a1, 3d
                    日志复制           :a3, after a2, 4d
                    section 提交阶段
                    日志提交           :a4, after a3, 2d
            </div>
        </section>

        <!-- Comparison Section -->
        <section class="mb-20">
            <div class="flex items-center mb-10">
                <div class="bg-indigo-500 w-2 h-12 mr-4 rounded-full"></div>
                <h2 class="text-3xl font-bold">协议对比</h2>
            </div>

            <div class="overflow-x-auto">
                <table class="min-w-full bg-white rounded-lg overflow-hidden shadow-md">
                    <thead class="bg-indigo-50">
                        <tr>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">特性</th>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">Paxos</th>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">Raft</th>
                            <th class="py-3 px-4 text-left font-semibold text-gray-700">Zab</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        <tr class="hover:bg-gray-50">
                            <td class="py-3 px-4 font-medium">一致性强度</td>
                            <td class="py-3 px-4 text-indigo-600">强一致性</td>
                            <td class="py-3 px-4 text-indigo-600">强一致性</td>
                            <td class="py-3 px-4 text-indigo-600">强一致性</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="py-3 px-4 font-medium">实现复杂度</td>
                            <td class="py-3 px-4 text-red-500">高</td>
                            <td class="py-3 px-4 text-green-500">低</td>
                            <td class="py-3 px-4 text-red-500">高</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="py-3 px-4 font-medium">性能</td>
                            <td class="py-3 px-4 text-amber-500">中等</td>
                            <td class="py-3 px-4 text-amber-500">中等</td>
                            <td class="py-3 px-4 text-green-500">高</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="py-3 px-4 font-medium">适用场景</td>
                            <td class="py-3 px-4">理论验证、复杂系统</td>
                            <td class="py-3 px-4">大多数分布式系统</td>
                            <td class="py-3 px-4">Zookeeper等协调服务</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-semibold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">探索分布式系统的核心技术</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-indigo-300 hover:text-white transition-colors duration-200 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        <span>访问技术小馆</span>
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        // 初始化Mermaid图表
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            sequence: {
                useMaxWidth: true,
                height: 50,
                actorFontSize: 14,
                noteFontSize: 14,
                messageFontSize: 16
            },
            gantt: {
                useMaxWidth: true,
                barHeight: 30,
                fontSize: 14
            }
        });
    </script>
</body>
</html>